package user

import (
	"context"
	"go-zero-project/services/user/api/internal/svc"
	"go-zero-project/services/user/api/internal/types"
	"go-zero-project/services/user/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type InfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InfoLogic {
	return &InfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *InfoLogic) UserInfo(req *types.UserInfoReq) (resp *types.UserInfoResp, err error) {
	// builder rpc request
	queryInfoResp := pb.QueryUserInfoReq{UserId: req.UserId}
	// request rpc
	queryUserInfo, err := l.svcCtx.UserCenterClient.QueryUserInfo(l.ctx, &queryInfoResp)
	if err != nil {
		return nil, err
	}
	// return obj
	resp = new(types.UserInfoResp)
	roleArr := queryUserInfo.RoleArr
	var roleArrResp []types.RoleInfoResp
	for _, v := range roleArr {
		roleArrResp = append(roleArrResp, types.RoleInfoResp{RoleId: v.RoleId, RoleName: v.RoleName, RoleDesc: v.RoleDesc})
	}
	resp.RoleArr = roleArrResp
	resp.UserId = queryUserInfo.UserId
	resp.UserName = queryUserInfo.UserName
	resp.Mobile = queryUserInfo.Mobile
	resp.Email = queryUserInfo.Email
	resp.Status = queryUserInfo.Status
	resp.DeptId = queryUserInfo.DeptId
	resp.DeptName = queryUserInfo.DeptName
	resp.CreateTime = queryUserInfo.CreateTime
	resp.UpdateTime = queryUserInfo.UpdateTime
	return resp, nil
}
